﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FuckData.BO;
using Newtonsoft.Json;
using FuckData.Model;
using Newtonsoft.Json.Linq;

namespace FuckServices.Controllers
{
    public class ServicesController : Controller
    {
        CategoryBO _CategoryBO = new CategoryBO();
        AccountBO _AccountBO = new AccountBO();
        PlaceBO _PlaceBO = new PlaceBO();
        ShareBO _ShareBO = new ShareBO();
        CommentBO _CommentBO = new CommentBO();
        [HttpPost]
        public ActionResult GETCATEGORY()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                List<CategoryModel> _ListCategory = _CategoryBO.GetListCategory();
                response.ListCategory = JsonConvert.SerializeObject(_ListCategory);
                response.result_code = "OK";
                response.tag = "GETCATEGORY";
                return Content(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                response.result_code = "ERROR";
                response.mess = ex.ToString();
                response.tag = "GETCATEGORY";
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult LOGIN()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                string UserName = Request.Form["UserName"].ToString();
                string Password = Request.Form["Password"].ToString();
                response.Infor = _AccountBO.CheckLogin(UserName, Password);
                response.result_code = "OK";
                response.tag = "LOGIN";
                return Content(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                response.result_code = "ERROR";
                response.mess = ex.ToString();
                response.tag = "LOGIN";
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult REGISTER()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                if (Request.Form["UserName"] != null && Request.Form["Email"] != null
                    && Request.Form["Password"] != null)
                {
                    tbl_Account _Account = new tbl_Account();
                    if (Request.Form["UserName"] != null)
                        _Account.UserName = Request.Form["UserName"].ToString();
                    if (Request.Form["Email"] != null)
                        _Account.Email = Request.Form["Email"].ToString();
                    if (Request.Form["Password"] != null)
                        _Account.Password = Request.Form["Password"].ToString();
                    if (_AccountBO.CheckUsername(_Account.UserName).ID != 0)
                    {
                        response.user_info = null;
                        response.result_code = "ERROR";
                        response.tag = "REGISTER";
                        return Content(JsonConvert.SerializeObject(response));
                    }
                    else if (_AccountBO.CheckEmail(_Account.Email).ID != 0)
                    {
                        response.user_info = null;
                        response.result_code = "ERROR";
                        response.tag = "REGISTER";
                        return Content(JsonConvert.SerializeObject(response));
                    }
                    else
                    {
                        _Account.Password = FuckData.Common.FunctionUtil.GetMD5(_Account.Password);
                        _Account.Status = 1;
                        _Account.Role = 1;
                        _AccountBO.Add(_Account);
                        long ID = _AccountBO.CheckUsername(_Account.UserName).ID;
                        response.user_info = ID;
                        response.result_code = "OK";
                        response.tag = "REGISTER";
                        return Content(JsonConvert.SerializeObject(response));
                    }
                }
                else
                {
                    response.user_info = null;
                    response.result_code = "ERROR";
                    response.tag = "REGISTER";
                    return Content(JsonConvert.SerializeObject(response));
                }
            }
            catch (Exception ex)
            {
                response.user_info = null;
                response.result_code = "ERROR";
                response.tag = "REGISTER";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult GETPLACE()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                if (Request.Form["CategoryID"] != null)
                {
                    long CategoryID = long.Parse(Request.Form["CategoryID"].ToString());
                    List<tbl_Place> _ListPlace = _PlaceBO.GetListPlaceByCat(CategoryID);
                    response.ListPlace = JsonConvert.SerializeObject(_ListPlace);
                    response.result_code = "OK";
                    response.tag = "GETPLACE";
                    return Content(JsonConvert.SerializeObject(response));
                }
                else
                {
                    response.ListPlace = null;
                    response.result_code = "ERROR";
                    response.tag = "GETPLACE";
                    return Content(JsonConvert.SerializeObject(response));
                }
            }
            catch (Exception ex)
            {
                response.ListPlace = null;
                response.result_code = "ERROR";
                response.tag = "GETPLACE";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult GETLISTTAXI()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                List<tbl_Place> _ListTaxi = _PlaceBO.GetListPlaceByCat(7);
                response.ListTaxi = JsonConvert.SerializeObject(_ListTaxi);
                response.result_code = "OK";
                response.tag = "GETLISTTAXI";
                return Content(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                response.ListTaxi = null;
                response.result_code = "ERROR";
                response.tag = "GETLISTTAXI";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult PLACEDETAIL()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                if (Request.Form["PlaceID"] != null)
                {
                    long PlaceID = long.Parse(Request.Form["PlaceID"].ToString());
                    tbl_Place _PLaceInfor = _PlaceBO.GetPlaceByID(PlaceID);
                    response.PLaceInfor = JsonConvert.SerializeObject(_PLaceInfor);
                    response.result_code = "OK";
                    response.tag = "PLACEDETAIL";
                    return Content(JsonConvert.SerializeObject(response));
                }
                else
                {
                    response.PLaceInfor = null;
                    response.result_code = "ERROR";
                    response.tag = "PLACEDETAIL";
                    return Content(JsonConvert.SerializeObject(response));
                }
            }
            catch (Exception ex)
            {
                response.PLaceInfor = null;
                response.result_code = "ERROR";
                response.tag = "PLACEDETAIL";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult SHAREPHOTO(HttpPostedFileBase FileUpload)
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                if (Request.Form["AccountID"] != null && FileUpload != null)
                {
                    long ID = long.Parse(Request.Form["AccountID"].ToString());
                    tbl_Account Account = _AccountBO.GetActiveByID(ID);
                    if (Account.ID != 0)
                    {
                        tbl_Share _Share = new tbl_Share();
                        _Share.AccountID = ID;
                        _Share.Comment = 0;
                        _Share.DateCreate = DateTime.Now;
                        _Share.Likes = 0;
                        _Share.Status = 0;
                        _Share.Title = Request.Form["Title"].ToString();
                        string HostName = Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port : "") + "/";
                        string fileName = Account.UserName + "_" + FileUpload.FileName;
                        string path = System.IO.Path.Combine(Server.MapPath("~/Uploads/Share/"), fileName);
                        FileUpload.SaveAs(path);
                        _Share.Title = "~/Uploads/Share/" + fileName;
                        _ShareBO.Add(_Share);
                        response.result_code = "OK";
                        response.tag = "SHAREPHOTO";
                        return Content(JsonConvert.SerializeObject(response));
                    }
                    else
                    {
                        response.result_code = "ERROR";
                        response.tag = "SHAREPHOTO";
                        return Content(JsonConvert.SerializeObject(response));
                    }
                }
                else
                {
                    response.result_code = "ERROR";
                    response.tag = "SHAREPHOTO";
                    return Content(JsonConvert.SerializeObject(response));
                }
            }
            catch (Exception ex)
            {
                response.result_code = "ERROR";
                response.tag = "SHAREPHOTO";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult GETLISTSHARE()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                List<ShareModel> _ListShare = _ShareBO.GetListShare();
                response.ListShare = JsonConvert.SerializeObject(_ListShare);
                response.result_code = "OK";
                response.tag = "GETLISTSHARE";
                return Content(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                response.ListShare = null;
                response.result_code = "ERROR";
                response.tag = "GETLISTSHARE";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
        [HttpPost]
        public ActionResult ADDCOMMENT()
        {
            dynamic response = JsonConvert.DeserializeObject("{}");
            try
            {
                tbl_Comment _Comment = new tbl_Comment();
                _Comment.Contents = Request.Form["Contents"].ToString();
                _Comment.DateCreate = DateTime.Now;
                _Comment.AccountID = long.Parse(Request.Form["AccountID"].ToString());
                _Comment.ShareID = long.Parse(Request.Form["ShareID"].ToString());
                _Comment.Status = 1;
                _CommentBO.Add(_Comment);
                response.result_code = "OK";
                response.tag = "ADDCOMMENT";
                return Content(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                response.result_code = "ERROR";
                response.tag = "ADDCOMMENT";
                response.mess = ex.ToString();
                return Content(JsonConvert.SerializeObject(response));
            }
        }
    }
}
